//
//  IntelligentRotateButton.m
//  HarmonizePub
//
//  Created by Haven on 16/10/13.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "IntelligentRotateButton.h"
#import "UIView+extension.h"
#import "Config.h"

@interface IntelligentRotateButton()
@property (nonatomic, assign) CGFloat titleH;
@end

@implementation IntelligentRotateButton
/**
 *  类方法创建按钮
 */
+ (instancetype)IntelligentRotateButton
{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 设置粗体
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        //设置按钮大小
        self.width = 40;
        
        //设置图片居中
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
 
        
        //设置图片
        [self setImage:[UIImage imageNamed:@"旋转.png"] forState:UIControlStateNormal];
        [self setTitle:@"转身" forState:UIControlStateNormal];
        [self setTitleColor: XHConfigColor forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont  systemFontOfSize:12];
    }
    return self;
}

// 修改title属性
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = self.width;
    CGFloat titleH = 0;
    // 宽度随文字变化
    NSDictionary *dict = @{
                           NSFontAttributeName : [UIFont systemFontOfSize:11.0]
                           };
    titleH = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options: NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
    self.titleH = titleH;
    titleY = self.height - titleH;
    return CGRectMake(titleX, titleY, titleW, titleH);
}

// 修改Image属性
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageX = 0;
    CGFloat imageW = self.width;
    CGFloat imageH = self.height - self.titleH;
    CGFloat imageY = 0;
    return CGRectMake(imageX, imageY, imageW, imageH);
}


@end
